package cos.parser;

import java.net.URL;
import java.util.List;
import java.util.regex.Pattern;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.Source;
import cos.entity.LyricsResponse;
import cos.entity.ParserName;
import cos.entity.Song;

public class LyricsWikiaParser implements Parser {

	private final String SITE_URL = "http://lyrics.wikia.com/";

	private final Pattern p = Pattern.compile(">\r\n");

	private final Pattern p2 = Pattern.compile("\\[phone]");

	@Override
	public LyricsResponse getLyrics(Song song) {
		String out = "";
		String sourceUrlString = SITE_URL + song.getArtistWithoutSpaces() + ":"
				+ song.getTitleWithoutSpaces();
		try {
			Source source = new Source(new URL(sourceUrlString));
			source.fullSequentialParse();

			// get content from <div class="lyricbox">..</div>
			// and delete html tags in it
			List<Element> linkElements = source
					.getAllElementsByClass("lyricbox");
			out = linkElements.get(0).getRenderer().toString();

			// delete unnecessary crap
			out = p2.split(p.split(out)[1])[0];

		} catch (Exception e) {
			// something went wrong - return null
			return null;
		}

		return new LyricsResponse(out, ParserName.LYRICS_WIKIA);
	}

}
